from com.arcfox.base.base_spider import BaseSpider
from com.arcfox.middleware.async_redis_middleware import open_redis
from com.arcfox.util import constant
from com.arcfox.util.util import get_md5


class YZYBaseSpider(BaseSpider):

    def __init__(self):
        super().__init__()
        self.data_version = None
        self.host = "uwf7de983aad7a717eb.youzy.cn"

    def get_u_sgin(self, data):
        salt = "9SASji5OWnG41iRKiSvTJHlXHmRySRp1"
        target = data + "&" + salt
        return get_md5(target.lower(), "UTF-8")

    @open_redis
    async def init_data_version(self, client):
        value = await client.get(constant.CONFIG_KEY_DATA_VERSION)
        if value:
            self.data_version = value.decode()

    def get_province_map(self):
        return {
            "安徽": 34,
            "北京": 11,
            "重庆": 50,
            "福建": 35,
            "广东": 44,
            "广西": 45,
            "甘肃": 62,
            "贵州": 52,
            "河北": 13,
            "河南": 41,
            "海南": 46,
            "湖北": 42,
            "湖南": 43,
            "黑龙江": 23,
            "吉林": 22,
            "江苏": 32,
            "江西": 36,
            "辽宁": 21,
            "内蒙古": 15,
            "宁夏": 64,
            "青海": 63,
            "上海": 31,
            "四川": 51,
            "山东": 37,
            "山西": 14,
            "陕西": 61,
            "天津": 12,
            "新疆": 65,
            "云南": 53,
            "浙江": 33
        }

    def get_province_map_reverse(self):
        return {
            34: "安徽",
            11: "北京",
            50: "重庆",
            35: "福建",
            44: "广东",
            45: "广西",
            62: "甘肃",
            52: "贵州",
            13: "河北",
            41: "河南",
            46: "海南",
            42: "湖北",
            43: "湖南",
            23: "黑龙江",
            22: "吉林",
            32: "江苏",
            36: "江西",
            21: "辽宁",
            15: "内蒙古",
            64: "宁夏",
            63: "青海",
            31: "上海",
            51: "四川",
            37: "山东",
            14: "山西",
            61: "陕西",
            12: "天津",
            65: "新疆",
            53: "云南",
            33: "浙江"
        }


if __name__ == "__main__":
    params = "{\"provinceCode\":34,\"score\":0,\"year\":2021,\"course\":\"文科\",\"bzType\":\"本专\"}"
    print(YZYBaseSpider().get_u_sgin(params))
    sss = '{"provincecode":34,"score":0,"year":2021,"course":"文科","bztype":"本专"}&9sasji5owng41irkisvtjhlxhmrysrp1'
    print(get_md5(sss))
